<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>活动模板列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <style>
        .layui-table-body .layui-table-cell {
            height: 70px;
            line-height: 70px;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <input type="text" name="title" placeholder="活动标题" autocomplete="off" class="layui-input">
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>

            </div>
        </div>



        <div class="layui-card-body">
            <table id="LAY-activity" lay-filter="LAY-activity"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add">添加活动</button>
                </div>
            </script>

            <script type="text/html" id="typeTpl">
                {{#  if(d.agent_id == 0){ }}
                <span class="layui-badge layui-bg-green">官方</span>
                {{#  } }}
                {{#  if(d.agent_id != 0){ }}
                <span class="layui-badge layui-bg-gray">自定义</span>
                {{#  } }}
            </script>

            <script type="text/html" id="obtainTpl">
                    {{#  if(d.item.vipdays == 0){ }}
                    <p>{{d.item.virtual_currency}}书币 + {{d.item.point_gift}}书点</p>
                    {{#  } }}
                    {{#  if(d.item.vipdays == 365){ }}
                    <p>包年</p>
                    {{#  } }}
                    {{#  if(d.item.vipdays == 30){ }}
                    <p>包月</p>
                    {{#  } }}
                    {{#  if(d.item.vipdays == 90){ }}
                    <p>包季</p>
                    {{#  } }}
            </script>

            <script type="text/html" id="imgTpl">
               <a href="javascript:;" onclick="showImg('{{d.img_text}}');"><img src="{{d.img_text}}" height="60" style="padding: 3px;border: 1px dashed #009688;"></a>
            </script>

            <script type="text/html" id="popTpl">
                <a href="javascript:;" onclick="showImg('{{d.pop_img_text}}');"><img src="{{d.pop_img_text}}" height="60" style="padding: 3px;border: 1px dashed #009688;"></a>
            </script>
            <script type="text/html" id="table-useradmin-admin">
                {{#  if(d.allow == 1){ }}
                <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                <a class="layui-btn layui-btn-primary layui-border-green layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                {{#  } else { }}
                <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-delete"></i>删除</a>
                <a class="layui-btn layui-btn-primary layui-border-green layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                {{#  } }}
            </script>
        </div>


    </div>

</div>

<div id="tong" class="layui-layer-wrap" style="display: none;">
    <a href="javascript:;" target="_blank">
        <img src="" id="showImg" style="height: 500px;">
    </a>
</div>


<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','admin'], function(){
        var $ = layui.$
            ,form = layui.form
            ,admin = layui.admin
            ,table = layui.table;

        form.on('submit(LAY-user-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('LAY-activity', {
                where: field
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });


        table.render({
            elem: '#LAY-activity'
            , url: '/recharge/activity' //模拟接口
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: false
            , even: true //开启隔行背景
            , response: {
                statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
            }
            , cellMinWidth: 100
            , page: true //开启分页
            , cols: [
                [{field: 'id', title: 'id', width: 80}
                    ,{field: 'title', title: '活动标题', minWidth: 250}
                    , {field: 'img', title: '图片',templet:'#imgTpl',align: 'center',width: 150}
                    , {field: 'totalFeeText', title: '充值金额',templet: function (d) {
                        return d.item.totalFeeText+' 元';
                    },align: 'center'}
                    ,{title: '套餐',  templet: '#obtainTpl',align: 'center'}
                    ,{title: '限购',templet:function (d) {
                        if (d.pay_times == 1){
                            return "1次"
                        }else {
                            return "不限"
                        }
                        },align: 'center'
                    }
                    , {field: 'type', title: '来源', templet: '#typeTpl', align: 'center'}
                    , {field: 'pop_img', title: '弹窗图片', templet: '#popTpl', align: 'center', width: 150}
                    , {
                    title: '操作',
                    minWidth: 200,
                    align: 'center',
                    fixed: 'right',
                    toolbar: '#table-useradmin-admin',
                }]]
        });


        //头工具栏事件
        table.on('toolbar(LAY-activity)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                {if $alias == 'channel'}
                    layer.msg('请使用上级代理账号添加');
                    return false;
                {/if}
                    layer.open({
                        type: 2
                        ,maxmin: true//开启最大化最小化按钮
                        ,title: '添加活动'
                        ,content: '/recharge/setActivity'
                        ,offset: '5px'
                        ,area: ['450px', '80%']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

                            //监听提交
                            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                //$.ajax({});
                                admin.req({
                                    url: '/recharge/setActivity'
                                    , type: "POST"
                                    , data: field
                                    , done: function (res) {
                                        layer.msg('添加成功', {icon: 1, time: 2000}, function () {
                                            table.reload('LAY-activity');
                                            layer.close(index); //关闭弹层
                                        });
                                    },not_finish:function (res) {
                                        layer.msg(res.msg);
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    break;
            }
        });


        //监听工具条
        table.on('tool(LAY-activity)', function(obj){
            var data = obj.data;
            if (obj.event === 'del'){
                layer.confirm('【'+obj.data.title+'】确认删除此活动吗？',{icon: 3, title:false}, function(index){
                    admin.req({
                        url: '/recharge/delActivity'
                        , type: "POST"
                        , data: {'id': obj.data.id}
                        , done: function (res) {
                            layer.msg('删除成功', {icon: 1, time: 1000}, function () {
                                obj.del();
                                layer.close(index);
                            });
                        },not_finish:function (res) {
                            layer.msg(res.msg);
                        }
                    });
                });
            }else if (obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,maxmin: true//开启最大化最小化按钮
                    ,title: '编辑活动'
                    , content: '/recharge/setActivity?id=' + obj.data.id
                    ,offset: '5px'
                    ,area: ['450px', '80%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            admin.req({
                                url: '/recharge/setActivity'
                                , type: "POST"
                                , data: field
                                , done: function (res) {
                                    layer.msg('设置成功', {icon: 1, time: 2000}, function () {
                                        table.reload('LAY-activity');
                                        layer.close(index); //关闭弹层
                                    });
                                },not_finish:function (res) {
                                    layer.msg(res.msg);
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }
        });
    });
</script>
<script>
    function showImg(imgUrl) {
        layui.$("#showImg").prop('src', imgUrl);
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            area: ['auto'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: layui.$('#tong')
        });
    }
</script>
</body>
</html>

